package cn.kgc.tangcco.http;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/13 下午3:16
 */
@WebServlet(urlPatterns = "/requestHeader")
public class RequestHeader extends HttpServlet {
    private static final long serialVersionUID = -1761781265450855219L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // System.out.println(request.getHeader("User-Agent"));
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        String userAgentString = request.getHeader("User-Agent");
        UserAgent ua =  UserAgentUtil.parse(userAgentString);
        System.out.println("浏览器 >>> " + ua.getBrowser().toString());
        System.out.println("浏览器版本号 >>> " + ua.getVersion().toString());
        System.out.println("浏览器内核 >>> " + ua.getEngine().toString());
        System.out.println("浏览器内核版本号 >>> " + ua.getEngineVersion().toString());
        System.out.println("操作系统 >>> " + ua.getOs().toString());
        System.out.println("操作系统 >>> " + ua.getPlatform().toString());
        if (ua.isMobile()){
            System.out.println("移动端");
        } else {
            System.out.println("PC端");
        }
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            System.out.println(request.getHeader(name));
        }
    }
}
